home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / kernel / fsutil / fsutilHandleScavenge.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-19  |  5.2 KB  |  206 lines

  1. /* 
  2.  * fsutilHandleScavenge.c --
  3.  *
  4.  *    Routines controlling the scavenging of file system handles.
  5.  *
  6.  * Copyright 1989 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /cdrom/src/kernel/Cvsroot/kernel/fsutil/fsutilHandleScavenge.c,v 9.2 91/09/10 18:24:34 rab Exp $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20.  
  21. #include <sprite.h>
  22.  
  23. #include <fs.h>
  24. #include <vm.h>
  25. #include <rpc.h>
  26. #include <fsutil.h>
  27. #include <fsprefix.h>
  28. #include <fsNameOps.h>
  29. #include <fsio.h>
  30. #include <fsStat.h>
  31. #include <sync.h>
  32. #include <timer.h>
  33. #include <proc.h>
  34. #include <hash.h>
  35. #include <fsrmt.h>
  36.  
  37.  
  38. /*
  39.  * Monitor for OkToScavenge and DoneScavenge
  40.  */
  41. static Sync_Lock scavengeLock = Sync_LockInitStatic("Fs:scavengeLock");
  42. #define LOCKPTR (&scavengeLock)
  43.  
  44. static Boolean OkToScavenge _ARGS_((void));
  45. static void DoneScavenge _ARGS_((void));
  46.  
  47.  
  48. /*
  49.  *----------------------------------------------------------------------------
  50.  *
  51.  * Fsutil_HandleScavengeStub --
  52.  *
  53.  *    This is a thin layer on top of Fsutil_HandleScavenge.  It is called
  54.  *    when L1-x is pressed at the keyboard, and also from Fsutil_HandleInstall
  55.  *    when a threashold number of handles have been created.
  56.  *
  57.  * Results:
  58.  *    None.
  59.  *
  60.  * Side effects:
  61.  *    Invokes the handle scavenger.
  62.  *
  63.  *----------------------------------------------------------------------------
  64.  *
  65.  */
  66. /*ARGSUSED*/
  67. void 
  68. Fsutil_HandleScavengeStub(data)
  69.     ClientData    data;    /* IGNORED */
  70. {
  71.     /*
  72.      * This is called when the L1-x keys are held down at the console.
  73.      * We set up a call to Fsutil_HandleScavenge, unless there is already
  74.      * an extra scavenger scheduled.
  75.      */
  76.     if (OkToScavenge()) {
  77.     Proc_CallFunc(Fsutil_HandleScavenge, (ClientData)FALSE, 0);
  78.     }
  79. }
  80.  
  81. Boolean        scavengerScheduled = FALSE;
  82. int        fsScavengeInterval = 2;            /* 2 Minutes */
  83. int        fsLastScavengeTime = 0;
  84.  
  85.  
  86. /*
  87.  *----------------------------------------------------------------------------
  88.  *
  89.  * Fsutil_HandleScavenge --
  90.  *
  91.  *    Go through all of the handles looking for clients that have crashed
  92.  *    and for handles that are no longer needed.  This expects to be
  93.  *    called by a helper kernel processes at regular intervals defined
  94.  *    by fsScavengeInterval.
  95.  *
  96.  * Results:
  97.  *    None.
  98.  *
  99.  * Side effects:
  100.  *    The handle-specific routines may remove handles.
  101.  *
  102.  *----------------------------------------------------------------------------
  103.  *
  104.  */
  105. /*ARGSUSED*/
  106. void
  107. Fsutil_HandleScavenge(data, callInfoPtr)
  108.     ClientData        data;            /* Whether to reschedule again
  109.                          */
  110.     Proc_CallInfo    *callInfoPtr;        /* Specifies interval */
  111. {
  112.     Hash_Search                hashSearch;
  113.     register    Fs_HandleHeader        *hdrPtr;
  114.  
  115.     /*
  116.      * Note that this is unsynchronized access to a global variable, which
  117.      * works fine on a uniprocessor.  We don't want a monitor lock here
  118.      * because we don't want a locked handle to hang up all Proc_ServerProcs.
  119.      */
  120.     fsLastScavengeTime = Fsutil_TimeInSeconds();
  121.  
  122.     Hash_StartSearch(&hashSearch);
  123.     for (hdrPtr = Fsutil_GetNextHandle(&hashSearch);
  124.      hdrPtr != (Fs_HandleHeader *) NIL;
  125.          hdrPtr = Fsutil_GetNextHandle(&hashSearch)) {
  126.      if (fsio_StreamOpTable[hdrPtr->fileID.type].scavenge !=
  127.          (Boolean (*)())NIL) {
  128.          (*fsio_StreamOpTable[hdrPtr->fileID.type].scavenge)(hdrPtr);
  129.      } else {
  130.          Fsutil_HandleUnlock(hdrPtr);
  131.      }
  132.     }
  133.     /*
  134.      * We are called in two cases.  A regular call background call is indicated
  135.      * by a TRUE data value, while an extra scavenge that is done in an
  136.      * attempt to free space is the other case.
  137.      */
  138.     if ((Boolean)data) {
  139.     /*
  140.      * Set up next background call.
  141.      */
  142.     callInfoPtr->interval = fsScavengeInterval * timer_IntOneMinute;
  143.     } else {
  144.     /*
  145.      * Indicate that the extra scavenger has completed.
  146.      */
  147.     callInfoPtr->interval = 0;
  148.     DoneScavenge();
  149.     }
  150. }
  151.  
  152. /*
  153.  *----------------------------------------------------------------------------
  154.  *
  155.  * OkToScavenge --
  156.  *
  157.  *    Checks for already active scavengers.  Returns FALSE if there
  158.  *    is already a scavenger.
  159.  *
  160.  * Results:
  161.  *    TRUE if there is no scavenging in progress.
  162.  *
  163.  * Side effects:
  164.  *    Sets scavengerScheduled to TRUE if it had been FALSE.
  165.  *
  166.  *----------------------------------------------------------------------------
  167.  *
  168.  */
  169. static ENTRY Boolean
  170. OkToScavenge()
  171. {
  172.     register Boolean ok;
  173.     LOCK_MONITOR;
  174.     ok = !scavengerScheduled;
  175.     if (ok) {
  176.     scavengerScheduled = TRUE;
  177.     }
  178.     UNLOCK_MONITOR;
  179.     return(ok);
  180. }
  181.  
  182. /*
  183.  *----------------------------------------------------------------------------
  184.  *
  185.  * DoneScavenge --
  186.  *
  187.  *    Called when done scavenging.  This clears the flag that indicates
  188.  *    an extra scavenger is present.
  189.  *
  190.  * Results:
  191.  *    None.
  192.  *
  193.  * Side effects:
  194.  *    Clears scavengerScheduled.
  195.  *
  196.  *----------------------------------------------------------------------------
  197.  *
  198.  */
  199. static ENTRY void
  200. DoneScavenge()
  201. {
  202.     LOCK_MONITOR;
  203.     scavengerScheduled = FALSE;
  204.     UNLOCK_MONITOR;
  205. }
  206.